<?php
namespace app\admin\controller;

use app\AdminCollection;
use app\model\Admin;
use app\model\AdminLoginLog;
use My\Tool;
use My\Validate;
use think\captcha\Captcha;
use think\Session;

class Login extends AdminCollection
{
    public function __construct()
    {
        parent::__construct();
    }

    public function index()
    {
        $indexurl = url('index/index');
        $submit = input('submit');
        if ($submit) {
            //$captcha = new Captcha();
            $input = input('');
            $data = $input['info'];
            $username = $data['username'];
            $password = $data['password'];
            if (!empty($data['remember'])) {
                $remember = $data['remember'];
            }
            //$verify = $data['verify'];
            //if (Validate::checkNull($verify)) $this->returnError("请输入验证码");
            //if (Validate::checkNull($captcha->check($verify))) $this->returnError("输入验证码错误");
            if (Validate::checkNull($username)) $this->returnError("请输入用户名");
            if (Validate::checkNull($password)) $this->returnError("请输入登录密码");
            $where = array();
            $where['username'] = $username;
            $adminFind = Admin::findService($where);
            if (Validate::checkEmpty($adminFind)) $this->returnError("用户名不存在");
            $encryption = Tool::encryptionFunction($password, $adminFind['salt']);
            if (Validate::checkNoEqual($adminFind['password'], $encryption)) $this->returnError("登录密码错误");
            $errorStatus = [0 => '登录账号已经禁用', 2 => '登录账号异常'];
            if ($adminFind['status'] != 1) $this->returnError($errorStatus[$adminFind['status']]);
            $time = time();
            $ip = getIP();
            //session保存登录信息
            if (!empty($remember)) {
                $info['username'] = $username;
                $info['password'] = $password;
                $info['remember'] = $remember;
                $info['time'] = time();
                Session::set('info', $info);
            } else {
                Session::delete('info');
            }
            //登录生成session
            $session_data = array();
            $session_data['admin_id'] = $adminFind['admin_id'];
            $session_data['username'] = $adminFind['username'];
            $session_data['group_id'] = $adminFind['group_id'];
            $session_data['status'] = $adminFind['status'];
            $session_data['login_count'] = $adminFind['login_count'];
            $session_data['login_ip'] = $ip;
            $session_data['login_time'] = $time;
            $this->putSession($session_data);
            //登录修改资料
            Admin::updateLoginServer($where);
            //登录记录
            $log = array();
            $log['admin_id'] = $adminFind['admin_id'];
            $log['admin_username'] = $adminFind['username'];
            $log['login_time'] = $time;
            $log['login_ip'] = $ip;
            AdminLoginLog::addService($log);
            $this->returnSuccess("登录成功", $indexurl);
        } else {
            //获取session保存的值
            $info = Session::get('info');
            if (!empty($info)) {
                if (time() - $info['time'] > 86400 * 7) Session::delete('info');
                $this->assign('info', $info);
            }
            if ($this->getAdmin()) $this->toredirect($indexurl);
            return $this->fetch();
        }
    }

}
